
from msg import *
from SendToNeighbours import send_message_to_neighbors
import globals

def check_out (mp, userName):

    # remove the item from the list of the active users
    flag = False

    try :
        globals.globalActiveUserListLock.acquire()
        if userName in globals.globalActiveUserList :
            del globals.globalActiveUserList[userName]
            #for item in globalActiveUserList :
            #    if item.userName == userName :

            # Make and Send a message to every node that this user has checked-in
            data = userName
            msg = Message(mp.thisName, None, None, data, 'check_out_nodes', mp.thisName)
            send_message_to_neighbors(mp, msg);

            print "User checked out -> %s" % (userName)
            #    globalActiveUserList.remove(item)
            flag = True
            #    break
    finally :
        globals.globalActiveUserListLock.release()


    print "Global active user list after checking out is : "
    print globals.globalActiveUserList
    #for item in globalActiveUserList :
    #    print item.userName

    if (flag) :
        return True
    else :
        return False
